<template>
  <div>
    <el-dialog
      :title="info.isadd ? '添加商品规格' : '编辑商品规格'"
      :visible.sync="info.isshow"
      :before-close="cancel"
      center
    >
      <el-form :model="specsInfo" :rules="rules" ref="specsInfo">
        <el-form-item label="商品规格名称" :label-width="formLabelWidth" prop="specsname">
          <el-input v-model="specsInfo.specsname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          v-for="(item, idx) in attrArr"
          label="规格属性"
          :label-width="formLabelWidth"
          :key="idx"
        >
          <el-input v-model="item.value" style="width:75%" autocomplete="off"></el-input>
          <el-button v-if="idx == 0" type="primary" @click="addArr">新增规格属性</el-button>
          <el-button v-else type="danger" @click="delArr(idx)">删除</el-button>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch
            v-model="specsInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button v-if="info.isadd" type="primary" @click="addSpecs">添 加</el-button>
        <el-button v-else type="primary" @click="editSpecs">编 辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//引入封装好的接口
import {
  specsAdd,
  specsInfo,
  specsEdit
} from "../../util/request/api";
import { mapActions} from "vuex";
export default {
  data() {
    return {
      specsInfo: {
        specsname: "",
        attrs: "",
        status: 1 
      },
      formLabelWidth: "120px",
      rules: {
        specsname: [
          { required: true, message: "请输入商品规格名称", trigger: "blur" },
          {
            min: 2,
            max: 15,
            message: "长度在 2 到 15 个字符",
            trigger: "blur"
          }
        ]
      },
      attrArr: [
        {
          value: ""
        }
      ]
    };
  },
  props: ["info"],
  methods: {
    //封装一个动态添加input事件
    addArr() {
      // 最多只能添加5个
      if (this.attrArr.length < 5) {
        this.attrArr.push({
          value: ""
        });
      } else {
        this.$message.warning("别添加了！累不累啊！！");
      }
    },
    //封装一个删除input 事件
    delArr(index) {
      this.attrArr.splice(index, 1);
    },
    ...mapActions({
      getSpecsList: "specs/getSpecsAction",
      getCount: "specs/getCountAction"
    }),
    //封装一个取消事件
    cancel() {
      this.$emit("cancel", false);
      this.reset();
    },
    //封装一个清空事件
    reset() {
      this.specsInfo = {
        specsname: "", 
        attrs: "",
        status: 1
      };
      this.$refs.specsInfo.resetFields();
      this.attrArr = [
        {
          value: ""
        }
      ];
    },
    // 确定事件
    addSpecs() {
      this.$refs.specsInfo.validate(valid => {
        if (valid) {
          console.log(this.specsInfo, "规格属性表单");
          console.log(this.attrArr, "动态表单数组");
          this.specsInfo.attrs = this.attrArr.map(item => item.value).join(",");
          specsAdd(this.specsInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              //编辑成功之后关闭弹框 重新渲染列表
              this.cancel();
              this.getSpecsList();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    },
    //封装一个查询一条数据的接口
    lookup(id) {
      specsInfo({ id }).then(res => {
        console.log(res, "规格属性的一条数据");
        this.specsInfo = res.list[0];
        this.specsInfo.attrs.map((item, index) => {
          console.log(item, "item");
          if (index == 0) {
            this.attrArr[0].value = item;
          } else {
            this.attrArr.push({
              value: item
            });
          }
        });
      });
    },
    // 编辑事件
    editSpecs() {
      this.$refs.specsInfo.validate(valid => {
        if (valid) {
          this.specsInfo.attrs = this.attrArr.map(item => item.value).join(",");
          specsEdit(this.specsInfo).then(res => {
            if (res.code == 200) {
              this.$message.success(res.msg);
              this.cancel();
              this.getSpecsList();
              this.getCount();
            } else {
              this.$message.error(res.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
          return false;
        }
      });
    }
  }
};
</script>

<style lang="" scoped></style>
